﻿using System;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace HY.Common
{
    public class ImageConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] img = null;
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                //img = (byte[])value;
                img = (byte[])System.Convert.ChangeType(value, typeof(byte[]));
            }
            if (img == null)
            {
                //return "/HDMSWpfManage;component/templet/images/defaulthead.jpg";
                return new BitmapImage(new Uri("Resources/noimage.jpg", UriKind.Relative));
            }
            return BitmapFrame.Create(new MemoryStream(img), BitmapCreateOptions.None, BitmapCacheOption.Default);
            //return ShowSelectedIMG(img);//以流的方式显示图片的方法
        }
        //转换器中二进制转化为BitmapImage  datagrid绑定显示的
        //private static BitmapImage ToImage(byte[] img)
        //{
        //    System.IO.MemoryStream ms = new System.IO.MemoryStream(img);
        //    ms.Seek(0, System.IO.SeekOrigin.Begin);
        //    BitmapImage newBitmapImage = new BitmapImage();
        //    newBitmapImage.BeginInit();
        //    newBitmapImage.StreamSource = ms;
        //    newBitmapImage.EndInit();
        //    return newBitmapImage;
        //}
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
